package cn.web.admin;

import cn.constant.ApiConstant;
import cn.constant.Messages;
import cn.entity.User;
import cn.service.RealNameService;
import cn.service.UserService;
import cn.web.model.ApiResult;
import cn.web.model.VmUser;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author m  2023/1/27 - 14:20
 * Description:
 */
@Api(tags = "管理员业务")
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminApiController {

    private final UserService userService;
    private final RealNameService realNameService;

    @Autowired
    public AdminApiController(UserService userService, RealNameService realNameService) {
        this.userService = userService;
        this.realNameService = realNameService;
    }

    //获取所有用户
    @ApiOperation("获取所有用户")
    @GetMapping(value = ApiConstant.ROUTE_USER_ALL)
    public ApiResult getUsers(@RequestParam String token){
        List<VmUser> users = new ArrayList<>();
        userService.get()
                .forEach(user -> users.add(VmUser.create(user,true)));
        return new ApiResult(true,Messages.SUCCESS.toString(),users);
    }

    //分页获取用户
    @ApiOperation("分页获取用户")
    @GetMapping(value = ApiConstant.ROUTE_USER_PAGE)
    public ApiResult getUsersPage(@RequestParam Integer page,@RequestParam Integer size,@RequestParam String token){
        List<VmUser> vmUsers = new ArrayList<>();
        userService.get(page,size).forEach(user -> vmUsers.add(VmUser.create(user,true)));
        return new ApiResult(true, Messages.SUCCESS.toString(),vmUsers);
    }

    //条件查找用户
    @ApiOperation("条件查找用户")
    @PostMapping(value = ApiConstant.ROUTE_USER_SEARCH)
    public ApiResult search(@PathVariable String kw,@RequestParam String token){
        List<VmUser> vmUsers = new ArrayList<>();
        userService.search(kw)
                .forEach(user -> vmUsers.add(VmUser.create(user,true)));
        return new ApiResult(true, Messages.SUCCESS.toString(),vmUsers);
    }

    //查询认证列表
    @ApiOperation("查询认证列表")
    @GetMapping(value = ApiConstant.ROUTE_USER_APPLY)
    public ApiResult apply(@RequestParam String token){
        List<VmUser> vmUsers= new ArrayList<>();
        userService.isApplying(true)
                .forEach(user -> vmUsers.add(VmUser.create(user,true)));
        return new ApiResult(true, Messages.SUCCESS.toString(),vmUsers);
    }


    //认证
    @ApiOperation("同意用户认证")
    @PostMapping(value = ApiConstant.ROUTE_USER_APPROVE)
    public ApiResult approve(@RequestParam UUID id, @RequestParam String token){
        User user = userService.approveUser(id);
        if (user == null){
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true, "", VmUser.create(user, true));
    }

    //拒绝
    @ApiOperation("拒绝用户认证")
    @PostMapping(value = ApiConstant.ROUTE_USER_DECLINED)
    public ApiResult declined(@RequestParam UUID id, @RequestParam String token){
        User user = userService.declinedUser(id);
        if (user == null){
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true, "", VmUser.create(user, true));
    }

    //禁止
    @ApiOperation("拉黑用户")
    @PostMapping(value = ApiConstant.ROUTE_USER_PROHIBIT)
    public ApiResult prohibit(@RequestParam UUID id, @RequestParam String token){
        User user = userService.prohibitUser(id);
        if (user == null){
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true, "", VmUser.create(user, true));
    }

}
